package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.DateUtils;
import com.letv.whatslive.model.Device;
import com.letv.whatslive.model.ForbiddenDevice;
import com.letv.whatslive.model.ForbiddenDevice;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBRef;

/**
 * Created by shenyue1 on 16-1-13.
 */
public class ForbiddenDeviceConvert {

    public static DBObject castForbiddenDeviceToDBObject(ForbiddenDevice forbiddenDevice) {

        DBObject dbo = new BasicDBObject();

        // 主键ID
        dbo.put("_id", ObjUtils.toLong(forbiddenDevice.getId(), 0L));
        // 被屏蔽设备ID
        dbo.put("deviceId", ObjUtils.ifNull(forbiddenDevice.getDeviceId(), ""));
        // 被屏蔽时间
        dbo.put("createTime", ObjUtils.toLong(forbiddenDevice.getCreateTime(), System.currentTimeMillis()));

        return dbo;
    }

    public static ForbiddenDevice castDBObjectToForbiddenDevice(DBObject dbObject) {

        if(dbObject != null) {

            ForbiddenDevice forbiddenDevice = new ForbiddenDevice();

            // 主键ID
            forbiddenDevice.setId(ObjUtils.toLong(dbObject.get("_id")));
            // 被屏蔽设备ID
            forbiddenDevice.setDeviceId(ObjUtils.toString(dbObject.get("deviceId")));
            // 被屏蔽时间
            forbiddenDevice.setCreateTime(DateUtils.long2YMDHMS(ObjUtils.toLong(dbObject.get("createTime"))));

            return forbiddenDevice;
        } else {

            return null;
        }
    }
}
